#include "OBB.h"
#include "frustum.h"

OBB::OBB()
{
}

void OBB::GenerateBoundingBox( std::vector<D3DXVECTOR3> &vrts )
{
	D3DXVECTOR3 min = D3DXVECTOR3( FLT_MAX, FLT_MAX, FLT_MAX );
	D3DXVECTOR3 max = D3DXVECTOR3( -FLT_MAX, -FLT_MAX, -FLT_MAX );

	for( size_t i = 0; i < vrts.size(); ++i )
	{
		min.x = min( min.x, vrts[i].x );
		min.y = min( min.y, vrts[i].y );
		min.z = min( min.z, vrts[i].z );

		max.x = max( min.x, vrts[i].x );
		max.y = max( min.y, vrts[i].y );
		max.z = max( min.z, vrts[i].z );
	}

	vertices[0] = min;
	vertices[1] = D3DXVECTOR3( max.x, min.y, min.z );
	vertices[2] = D3DXVECTOR3( max.x, max.y, min.z );
	vertices[3] = D3DXVECTOR3( min.x, max.y, min.z );

	vertices[4] = max;
	vertices[5] = D3DXVECTOR3( min.x, max.y, max.z );
	vertices[6] = D3DXVECTOR3( min.x, min.y, max.z );
	vertices[7] = D3DXVECTOR3( max.x, min.y, max.z );
}

D3DXVECTOR3 *OBB::GetVertices()
{
	for( size_t i = 0; i<8; ++i )
	{
		D3DXVECTOR4 tmp;
		D3DXVec3Transform( &tmp, &vertices[i], &transform );
		vertices[i].x = tmp.x / tmp.w;
		vertices[i].y = tmp.y / tmp.w;
		vertices[i].z = tmp.z / tmp.w;
	}
	return vertices;
}